
<!--A Design by W3layouts
Author: W3layout
Author URL: http://w3layouts.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html>
<head>
<title>iQ Online Survey System| Home </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link href="css/style.css" media="all" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>//publish
$(document).ready(function(){

  var serverAddress = '127.0.0.1';

  $("#publish_id").click(function(){
	  var survey_id= document.getElementById("publish_survey_id").value;
	  var survey_status_id=2;
	  
    var request = $.ajax({
        url: "http://" + serverAddress + "/soen-6461-server/survey/update",
        type: "post",
        data : {
				"Surveys[survey_id]" :survey_id,
				"Surveys[status_id]":survey_status_id
				
			}
    });

	request.done(function (response, textStatus, jqXHR){
         if(response.code==100)
	   {
		   
		document.getElementById("publish_response").innerHTML="Survey is ready to be used by participant, give the ID to participant to access the survey ";
	   }
	   else  if(response.code==-100)
	   {
		   document.getElementById("publish_response").innerHTML="Survey is wrong ";
	   }
		
		
    });
	
  });
});
</script>

</head>

<body>
<div class="header-bg">
<div class="wrap">
	<div class="header">
		<div class="logo">
		  		<a href="index.html"><b><font color="#00FF33" size="+3">iQ Online Survey System</font></b> </a>
		 </div>
	<div class="drp-dwn">
	 	<select onchange="window.location=this.options[this.selectedIndex].value">
			<option value="index.html">Home</option>
			<option value="signin.html">Signin</option>
			<option value="participate_in_survey.html">Participate In survey </option>
			<option value="about.html">About</option>
		</select>
	</div>
	 <div class="clear"></div>
	</div>
</div>
</div>
<div class="fluid_container">

<form action="" method="get" name="user_options">
  <p>
    <select style="float:left" name="options" id="chosenOption">
    <option> Choose an option</option>
      <option value="1">Create a Survey</option>
      <option value="2">Delete a Survey</option>
      <option value="3">publish a Survey</option>
      <option value="4">View Results of a Survey</option>
    </select>
  </p>
  <p><a href="index.html" style="float:right"><font size="2" >logout</font></a></p>
</form>
<div id="delete" style="display:none">
	<form action="" method="get" name="delete_survey">

		<input name="delete_survey_id" type="text" value="Enter Survey Name"  onclick="value=''"/>
		<input type="submit" name="delete_id" id="delete_id" value="Delete"  onclick=""/>
	  </p>
	</form>
</div>
<div id="publish" style="display:none">
<form action="" method="get" name="delete_survey">

    <input id="publish_survey_id" type="text" value="Enter Survey Name"  onclick="value=''"/>
    <input type="button" name="publish_id" id="publish_id" value="Publish"/>

</form>
<div id="publish_response"></div>
</div>
<div id="results" style="display:none">
<form action="" method="get" name="get_results_survey">

    <input id="survey_id" type="text" value="Enter Survey ID"  onclick="value=''"/>
    <input type="button" name="survey_id" id="getResultsButton" value="Get Results"  />
</form>
<div id="results_res" style="visibility:hidden">
<table id="result_table" style="border:1px solid #000000;">
<tr class="border_bottom">
    <th>Questions</th>
    <th>Answer</th>
	<th>Answer Count</th>
  </tr>
</table>
</div>
</div>
</div>
<div class="footer-bg">
<div class="wrap">
<div class="footer">

		<div class="f_grid1">
			<ul class="f_nav">
				<li class="active"><a href="index.html">Home</a></li>
				<li><a href="signin.html">Signin</a></li>
				<li><a href="participate_in_survey.html">Participate In survey</a></li>
				<li><a href="about.html">About</a></li>
			</ul>
			<div class="copy">
				<p class="w3-link">© All Rights Reserved | Design by&nbsp; <a href="http://w3layouts.com/"> W3Layouts</a></p>
			</div>
			<div class="clear"></div>
		</div>
</div>
</div>
</div>
<script type="text/javascript">
	$('#chosenOption').change(
			function()
			{
					var choice = document.getElementById("chosenOption").value;
					
					if(choice==1)
					{
						window.location="./create_survey.html"
					}
					else if(choice==2)
					{
						$("#delete").show();
						$("#publish").hide();	
						$("#results").hide();
					}
					else if(choice==3)
					{
						$("#delete").hide();
						$("#publish").show();	
						$("#results").hide();
					}
					else if(choice==4)
					{
						$("#delete").hide();
						$("#publish").hide();	
						$("#results").show();
					}
					else 
					{
						window.alert("Error");
					}
					
			}
  );
  
  var serverAddress = 'localhost';

  $("#getResultsButton").click(function(){
	
	var id= document.getElementById("survey_id").value;
    var request = $.ajax({
        url: "http://" + serverAddress + "/soen-6461-server/surveyParticipation/results",
        type: "GET",
		dataType: "json",
        data : {
				survey_id:id
			}
    });

	request.done(function (response, textStatus, jqXHR){
        $('#response').text(JSON.stringify(response));
		
		var questionArray = [];
		for(var i=0 ; i<response.length;i++)
		{
			var qID = response[i].question_id;
			var qText = response[i].question_text;
			var aID = response[i].chosen_answer_id;
			var aText = response[i].answer_text;
			
			var questionObject = {qid:qID, qtext:qText};
			questionObject.answerChoices = [];
			var answerChoice = {aid:aID, atext:aText, count:1};
			questionObject.answerChoices.push(answerChoice);
			
			if(questionArray.length == 0){
				questionArray.push(questionObject);
			} else {
				var questionFound = false;
				for(var j = 0; j < questionArray.length; j++) {
					var question = questionArray[j];
					if(question.qid == qID) {
						questionFound = true;
						if(question.answerChoices == null || question.answerChoices == undefined) {
							question.answerChoices = [];
							var answerChoice = {aid:aID, atext:aText, count:1};
							question.answerChoices.push(answerChoice);
						} else {
							var answerFound = false;
							for(var k = 0; k < question.answerChoices.length; k++) {
								var answerChoice = question.answerChoices[k];
								if(answerChoice.aid == aID){
									answerChoice.count++;
									answerFound = true;
								}
							}
							
							if(!answerFound) {
								var newAnswerChoice = {aid:aID, atext:aText, count:1};
								question.answerChoices.push(newAnswerChoice);
							}
						}
				
					}
				}
				
				
				if(!questionFound) {
					questionArray.push(questionObject);
				}
			}
			
			
		}
		
		console.log(questionArray);
		
		var myTable = document.getElementById("result_table");
var rowCount = myTable.rows.length;
for (var x=rowCount-1; x>0; x--) {
   myTable.deleteRow(x);
}
		
		
		
		document.getElementById("results_res").style="visibility: visible";
		
		var table=document.getElementById("result_table");
	
		for(var i=0 ; i<questionArray.length;i++)
		{
			var row=table.insertRow(1);
			var cell1=row.insertCell(0);
			var cell2=row.insertCell(1);
			var cell3=row.insertCell(2);
			cell1.innerHTML=questionArray[i].qtext;
			cell2.innerHTML=questionArray[i].answerChoices[0].atext;
			cell3.innerHTML=questionArray[i].answerChoices[0].count;
			
		}
		
    });
	//print the questionArray
	//results_res
	//result_table
	

  });
</script>
</body>
</html>